<?php
require_once ACTION_PATH.'common/filter.class.php';
/**
 * model基类
 */
abstract class ModelBase1 {
	protected $db = '';
	
	protected $baseurl = '';
	
	public final function __construct() {
		$this->init();
		
		/*
		 * $method = isset ( $_REQUEST['method'] ) ? $_REQUEST['method'] :
		 * 'action'; $this -> $method ( );
		 */
	}
	
	protected function init($dbname =''){
		$db = new dbstuff ();
		if ($dbname=='ienjoy'){
			$this->db = $db->dbienjoyconnect ();
		}else{
			$this->db = $db->dbconnect ();
		}
	}
	public function __call($name, $arguments) {
		/*
		 * $this -> action ( );
		 */
	}
	
	// 共获取内容方法
	public function getTable($tblname, $wherearr=0,$limit=0,$prelimit=0,$orderby='') {
		$array = array();
		$where = '';
		$limitsql = $orderby;
		if ($wherearr) {
			$num = 0;
			foreach ( $wherearr as $k => $v ) {
				if ($num == 0) {
					$where = ' where ';
				}
				
				if ($v || ($v == 0)) {
					if ($num != 0) {
						$where .= ' and ';
					}
					
					$where .= $k . "='" . $v . "'";
					
					$num ++;
				}
			}
		}
		
		
		if($prelimit>0){
			$limitsql .= " limit $limit , $prelimit";
		}
		
			
			global $_SGLOBAL;
		        $sql = "SELECT * FROM $tblname" . $where.$limitsql;
				$_SGLOBAL ['db']->query ( $sql );
			
			while ( $value = $_SGLOBAL ['db']->fetch_row() ) {
				$array [] = ($value);
			}
				
			return $array;
		
	}
	
	// 共获取内容方法
	public function searchTable($tblname, $wherearr=0,$limit=0,$prelimit=0,$orderby='') {
		$array = array();
		$where = '';
		$limitsql = $orderby;
		if ($wherearr) {
			$num = 0;
			foreach ( $wherearr as $k => $v ) {
				if ($num == 0) {
					$where = ' where ';
				}
	
				if ($v || ($v == 0)) {
					if ($num != 0) {
						$where .= ' like ';
					}
						
					$where .= $k . " like '%" . $v . "%'";
						
					$num ++;
				}
			}
		}
	
	
		if($prelimit>0){
			$limitsql .= " limit $limit , $prelimit";
		}
	
			
		global $_SGLOBAL;
		$sql = "SELECT * FROM $tblname" . $where.$limitsql;
		$_SGLOBAL ['db']->query ( $sql );
			
		while ( $value = $_SGLOBAL ['db']->fetch_row() ) {
			$array [] = ($value);
		}
	
		return $array;
	
	}
	
	// 共获取数量方法
	public function getRcount($tblname, $wherearr=0) {
		
		$where = '';
		if ($wherearr) {
			$num = 0;
			foreach ( $wherearr as $k => $v ) {
				if ($num == 0) {
					$where = ' where ';
				}
		
				if ($v || ($v == 0)) {
					if ($num != 0) {
						$where .= ' and ';
					}
						
					 $where .= $k . "='" . $v . "'";
				}
			
					$num++;
			}
				
		}
		global $_SGLOBAL;
		$sql = "select count(*) as count from $tblname".$where;
		$query =$_SGLOBAL ['db']->query ( $sql );
		$count = $_SGLOBAL ['db']->fetch_row ();
		return $count ['count'];
	}
	
	// 共删除方法
	public function deleteTable($tblname, $wherearr) {
		
		if ($wherearr) {
			$where = '';
			$num = 0;
			
			foreach ( $wherearr as $k => $v ) {
				if ($num == 0) {
					$where = ' where ';
				}
				
				if ($v || ($v == 0)) {
					if ($num != 0) {
						$where .= ' and ';
					}
					
					$where .= $k . "='" . $v . "'";
					
					$num ++;
				}
			}
			
			global $_SGLOBAL;
			$sql = "DELETE FROM $tblname" . $where;
			$count = $_SGLOBAL ['db']->query ( $sql );
			return $count;
		} else {
			return 0;
		}
	}
	// 共添加&修改方法
	public function replaceTable($tblname, $addarr) {
		
		if ($addarr) {
			$value = $key = '';
			$num1 = 0;
			$num = count ( $addarr );
			foreach ( $addarr as $k => $v ) {
				if ($num == 1) {
					$key .= "`$k`";
					$value .= $v;
				} else {
					if ($num-1 == $num1) {
						$key .= "`$k`";
						$value .= "'".$v."'";
					} else {
						$key .= "`$k`,";
						$value .= "'".$v."',";
					}
				}
				$num1++;
			}
			
			global $_SGLOBAL;
			 $sql = "REPLACE `$tblname` ($key) VALUES( $value )";
			
			$count = $_SGLOBAL ['db']->query ( $sql );
			if($count){
				$insertid= $_SGLOBAL ['db']->insert_id();
				if($insertid){
					return $insertid;
				}else{
					return 1;
				}
			}
		} else {
			return 0;
		}
	}
	
	// 修改方法
	public function updateTable($tblname, $addarr,$wherearr) {
	
		if ($addarr) {
			$value = $key = '';
			$num1 = 0;
			$num = count ( $addarr );
			$updatesqtl = '';
			foreach ( $addarr as $k => $v ) {
				if ($num == 1) {
					$updatesqtl = $k.'='.$v;
				} else {
					if ($num-1 == $num1) {
						$updatesqtl .= "`$k`".'='."'".$v."'";
					} else {
						$updatesqtl .= "`$k`".'='."'".$v."',";
					}
				}
				$num1++;
			}
			$where = '';
			$num = 0;
			if($wherearr){
				foreach ( $wherearr as $k => $v ) {
					if ($num == 0) {
						$where = ' where ';
					}
				
					if ($v || ($v == 0)) {
						if ($num != 0) {
							$where .= ' and ';
						}
							
						$where .= $k . "='" . $v . "'";
							
						$num ++;
					}
				}
				
					
				global $_SGLOBAL;
				$sql = "UPDATE `$tblname` set".$updatesqtl.$where;
				$count = $_SGLOBAL ['db']->query ( $sql );
				if($count){
					$insertid= $_SGLOBAL ['db']->insert_id();
					if($insertid){
						return $insertid;
					}else{
						return 1;
					}
				}
			}else{
				return 0;
			}
		} else {
			return 0;
		}
	}

}
